package com.wwz.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.lang.tree.Tree;
import cn.hutool.core.lang.tree.TreeUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.wwz.entity.Company;
import com.wwz.entity.Department;
import com.wwz.entity.Result;
import com.wwz.exception.EcoBootException;
import com.wwz.mapper.DepartmentMapper;
import com.wwz.response.DeptList;
import com.wwz.service.ICompanyService;
import com.wwz.service.IDepartmentService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;
import java.util.stream.Collectors;

/**
 * @author Admin
 * @description 针对表【department】的数据库操作Service实现
 * @createDate 2022-09-07 23:55:56
 */
@Service
public class DepartmentServiceImpl extends ServiceImpl<DepartmentMapper, Department> implements IDepartmentService {

    @Resource
    private ICompanyService companyService;

    @Override
    public void addOrUpData(Department department) {

        if (StrUtil.isAllEmpty(department.getName(),department.getManager())) {
            throw new EcoBootException("字段不能为空");
        }

        Department one = getById(department.getId());
        if (one == null) {
            String companyId = "7ac32217-25bf-459a-b07d-b739c5c33210";
            department.setId(StrUtil.uuid());
            department.setCompanyId(companyId);
            department.setCreateTime(new Date());
            if (StrUtil.isEmpty(department.getPid())){
                department.setPid("0");
            }
            save(department);
            return;
        }
        updateById(department);
    }


    @Override
    public DeptList getList(String companyId) {
        if (StrUtil.isEmpty(companyId)) {
            throw new EcoBootException("企业id为空");
        }

        Company company = companyService.getById(companyId);
        if (company == null) {
            throw new EcoBootException("公司信息为空");
        }

        List<Department> departments = list(new LambdaQueryWrapper<Department>()
                .eq(Department::getCompanyId, companyId));
        if (CollUtil.isEmpty(departments)) {
            throw new EcoBootException("部门信息为空");
        }

        return new DeptList(company, departments);
    }

    @Override
    public void delete(String id) {
        List<String> departments = this.list(Wrappers.<Department>lambdaQuery()
                        .eq(Department::getId, id)
                        .or()
                        .eq(Department::getPid, id))
                .stream()
                .map(Department::getId)
                .collect(Collectors.toList());
        removeByIds(departments);
    }

    @Override
    public List<Tree<String>> getTreeList() {
        return getDeptTree(this.list(Wrappers.emptyWrapper()), "0");
    }

    /**
     * 构建树形结构
     *
     * @param depts
     * @param parentId
     * @return
     */
    private List<Tree<String>> getDeptTree(List<Department> depts, String parentId) {

        return TreeUtil.build(depts.stream()
                        .sorted(Comparator.comparing(Department::getCreateTime)
                                .reversed())
                        .collect(Collectors.toList()), parentId,
                (dept, tree) -> {
                    tree.setId(dept.getId());
                    tree.setName(dept.getName());
                    tree.setParentId(dept.getPid());
                    // 扩展字段
                    tree.putExtra("manager", dept.getManager());
                });
    }


}




